package dao.impl;

import dao.TeacherDao;
import domain.Course;
import domain.Teacher;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.BlockChainUtils;
import utils.JDBCUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TeacherDaoImpl implements TeacherDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int findTotalCount(Map<String, String[]> condition) {
        //定义模板初始化sql
        String sql = "select count(*) from teacher where 1=1";
//        String sql = "select * from teacher";
        StringBuilder sb = new StringBuilder(sql);
        //遍历map
        Set<String> keySet = condition.keySet();
        //定义参数集合
        List<Object> params = new ArrayList<Object>();
        for (String key : keySet) {
            //排除分页条件参数
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }

            //获取value
            String value = condition.get(key)[0];
            //判断value是否有值
            if (value != null && !"".equals(value)) {
                //有值
                sb.append(" and " + key + " like ? ");
                params.add("%" + value + "%");//?条件的值
            }
        }
        System.out.println(sb.toString());
        System.out.println(params);
        return template.queryForObject(sb.toString(), Integer.class, params.toArray());
    }

    @Override
    public List<Teacher> findByPage(int start, int rows, Map<String, String[]> condition) {
        try {
            String sql = "select * from teacher where 1=1";
            StringBuilder sb = new StringBuilder(sql);
            //遍历map
            Set<String> keySet = condition.keySet();
            //定义参数集合
            List<Object> params = new ArrayList<Object>();
            for (String key : keySet) {
                //排除分页条件参数
                if ("currentPage".equals(key) || "rows".equals(key)) {
                    continue;
                }

                //获取value
                String value = condition.get(key)[0];
                //判断value是否有值
                if (value != null && !"".equals(value)) {
                    //有值
                    sb.append(" and " + key + " like ? ");
                    params.add("%" + value + "%");//?条件的值
                }
            }
            //添加分页查询
            sb.append(" limit ? , ?");
            //添加分页查询参数值
            params.add(start);
            params.add(rows);
            System.out.println(sb.toString());
            System.out.println(params);
            return template.query(sb.toString(), new BeanPropertyRowMapper<Teacher>(Teacher.class), params.toArray());
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Teacher findTeacheridAndPassword(String id, String password) {
        try {
            String sql = "select * from teacher where t_id = ? and t_password = ?";
            Teacher teacher = template.queryForObject(sql, new BeanPropertyRowMapper<Teacher>(Teacher.class), id, password);
            teacher.setStudent(new StudentDaoImpl().findStudentById(teacher.getS_id()));
            return teacher;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Teacher> findAll() {
        try {
            String sql = "select * from teacher";
            List<Teacher> teachers = template.query(sql, new BeanPropertyRowMapper<Teacher>(Teacher.class));
            return teachers;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Course> findMySelfOptionalCourse(String t_id) {
        try {
            String sql = "select * from course where t_id = ?";
            List<Course> courses = template.query(sql, new BeanPropertyRowMapper<Course>(Course.class), t_id);
            return courses;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Course findOptionalCourseByCourseId(String cid) {
        try {
            String sql = "select * from course where c_id = ?";
            Course c = template.queryForObject(sql, new BeanPropertyRowMapper<Course>(Course.class), cid);
            return c;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void updateCourseInfo(Course updateCourse) {
        try {
            String sql = "update course set c_name =?,c_info=? where c_id=?";
            template.update(sql, updateCourse.getC_name(), updateCourse.getC_info(), updateCourse.getC_id());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteCourseById(String cid) {
        try {
            String sql = "delete from course where c_id=?";
            template.update(sql, cid);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updatePassword(String teacherid, String newpassword) {
        Teacher oldTeacher = this.findTeacherById(teacherid);
        try {
            String sql = "update teacher set t_password=? where t_id=?";
            template.update(sql, newpassword, teacherid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Teacher teacher = this.findTeacherById(teacherid);
        BlockChainUtils.set(teacherid,teacherid,oldTeacher.toString(),teacher.toString());
    }

    @Override
    public Teacher findTeacherById(String t_id) {
        try {
            String sql = "select * from teacher where t_id = ?";
            Teacher teacher = template.queryForObject(sql, new BeanPropertyRowMapper<Teacher>(Teacher.class), t_id);
            if (teacher.getS_id() != null) {
                teacher.setStudent(new StudentDaoImpl().findStudentById(teacher.getS_id()));
            }
            return teacher;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void addTeacherAllInfo(Teacher t) {
        try {
            String sql = "insert into teacher(t_id,t_name,t_sex,t_education,t_title) values(?,?,?,?,?)";
            template.update(sql, t.getT_id(), t.getT_name(), t.getT_sex(), t.getT_education(), t.getT_title());
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteTeacherById(String teacherid) {
        Teacher oldTeacher = this.findTeacherById(teacherid);
        try {
            String sql = "delete from teacher where t_id=?";
            template.update(sql, teacherid);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        BlockChainUtils.set(teacherid,teacherid,oldTeacher.toString(),null);
    }

    @Override
    public void updateInfo(Teacher t) {
        try {
            String sql = "update teacher set t_name =?,t_sex=?,t_education=?,t_title=?,t_password=?,s_id=? where t_id=?";
            template.update(sql, t.getT_name(), t.getT_sex(), t.getT_education(), t.getT_title(), t.getT_password(), t.getS_id(), t.getT_id());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
